package com.distribute.application.admin;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import com.distribute.framework.core.lang.Protocol;
import com.distribute.framework.core.lang.flag;
import com.distribute.framework.core.lang.limitVal;
import com.distribute.framework.core.web.helper.Utils;
import com.distribute.framework.core.web.servlet.upload.UpLoadServlet;

import com.distribute.framework.model.SrvLimited;

@Aspect
@Order(2)
@Configuration
public class AdminInterceptor{
	
	@Around("execution(public com.distribute.framework.core.lang.Protocol com.distribute.application.admin..*Controller.*(..))")
    public Object afterInvoke(ProceedingJoinPoint joinPoint) throws Throwable {
    	Utils.getServletResponse().setHeader("Access-Control-Allow-Origin", "*");//跨域设置
		Utils.getServletResponse().addHeader("JSESSIONID", Utils.getServletRequest().getSession().getId());
		if(LoginHelper.getUser()==null && Utils.getServletRequest().getServletPath().endsWith(".do")){
        	return Protocol.create(false, "对不起，请先登录...", "login_error");
        }
		Object controller = joinPoint.getTarget();
		flag classFlag = controller.getClass().getAnnotation(flag.class);
		MethodSignature ms = (MethodSignature)joinPoint.getSignature();
		limitVal limitVal = ms.getMethod().getAnnotation(limitVal.class);
		flag methodFlag = ms.getMethod().getAnnotation(flag.class);
		Map limitMap = new HashMap();
		if(classFlag!=null && limitVal!=null){
			limitMap = SystemHelper.getSrvLimit(classFlag.value(), LoginHelper.getUser());
			Integer limitValue = (Integer) limitMap.get("limitValue");
			if(limitValue!=null){
				if(limitVal.value()!=(limitValue & limitVal.value())){
					return Protocol.create(false, "对不起，没有权限操作此功能", "limit_error");
				}
			}
		}
		Protocol returnValue = (Protocol) joinPoint.proceed();
		if(returnValue!=null){
			returnValue.set("limitValue", limitMap.get("limitValue"));
			returnValue.getData().putAll(limitMap);
			returnValue.set("basePath", Utils.getRequest("basePath"));
			returnValue.set("UID", LoginHelper.getUserId());
			if(methodFlag!=null){
				Method getItemsMethod = controller.getClass().getMethod("getItems");
				if(getItemsMethod!=null){
					getItemsMethod.setAccessible(true);
					Map items = (Map) getItemsMethod.invoke(null);
					returnValue.getData().putAll(items);
				}
			}
		}
        return returnValue;
    }
    
    
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        return new ServletRegistrationBean(new UpLoadServlet(), "/admin/upload.do");// ServletName默认值为首字母小写，即myServlet1
    }
    
    
    @Bean
    public ServletRegistrationBean getCometServletBean() {
        return new ServletRegistrationBean(new CometServlet(), "/admin/comet");
    }
}